package com.by.controller;

import com.by.core.bean.ResultBean;
import com.by.core.controller.BaseController;
import com.by.entity.Minio;
import com.by.service.MinioService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/minio")
public class MinioController extends BaseController {
    @Autowired
    MinioService minioService;
    @Autowired
    Minio minio;
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public ResultBean upload(MultipartFile file) {
        String url = minioService.upload(minio.getBucketName(), file);
        return success("文件上传成功", url);
    }

    @ApiOperation("文件下载")
    @GetMapping("/download/{url}")
    public ResponseEntity<InputStreamResource> download(@PathVariable String url){
        ResponseEntity<InputStreamResource> download = minioService.download(url);
        return download;
    }

    @ApiOperation("文件删除")
    @DeleteMapping("/delete/{url}")
    public ResultBean delete(@PathVariable String url) {
        String delete = minioService.delete(url);
        return success("文件删除成功", delete);
    }
}
